import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
// import 'package:flutter_app_upgrade/flutter_app_upgrade.dart';
import 'package:flutter_module/api/version_model.dart';
import 'package:flutter_module/api/version_view_model.dart';
import 'package:flutter_module/commom/application.dart';
import 'package:flutter_module/commom/commom.dart';
import 'package:flutter_module/commom/data/const.dart';
import 'package:flutter_module/commom/user.dart';
import 'package:flutter_module/event/login_event.dart';
import 'package:flutter_module/http/UserModel.dart';
import 'package:flutter_module/http/api_service.dart';
import 'package:flutter_module/pages/home/home_page.dart';
import 'package:flutter_module/pages/login/drawer.dart';
import 'package:flutter_module/pages/lore/lore_page.dart';
//import 'package:flutter_module/pages/protect/protect_page.dart';
import 'package:flutter_module/pages/rumor/rumor_page.dart';
import 'package:flutter_module/pages/settings/RandomWordsState.dart';
import 'package:flutter_module/pages/settings/machine_settings.dart';
import 'package:flutter_module/widget/dialog/update_dialog.dart';
import 'package:flutter_module/widget/root_tabbar.dart';
import 'package:oktoast/oktoast.dart';
import 'package:package_info/package_info.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../DemoLocalizations.dart';
import '../../HYLocalizations.dart';

class RootPage extends StatefulWidget {
  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  @override
  void initState() {
    super.initState();
    print("start 3");
    checkVersion();
  }

  //Future<Null>
  _login(BuildContext context) async {
    //Fluttertoast.showToast(msg: "自动登录中！");
    //showToast("自动登录中！");
    SharedPreferences sp = await SharedPreferences.getInstance();
    String username = sp.getString("userName");
    String password = sp.getString("password");
    if ((null != username && username.trim().length > 0) &&
        (null != password && password.trim().length > 0)) {
      ApiService().login((UserModel _userModel, Response response) {
        if (_userModel != null) {
          print(_userModel.message);
          User().saveUserInfo(_userModel, response, password);
          Application.eventBus.fire(new LoginEvent());
          if (_userModel.success) {
          } else {
            showToast(_userModel.message);
          }
        }
      }, username, password);
    } else {
      //Fluttertoast.showToast(
      //  msg: "自动登录失败,请先登录!",
      //);
      showToast(DemoLocalizations.of(context).autoLoginFail);
    }
  }

  /// 检查更新 [check update]
  checkVersion() async {
    // if(false){
    // var AppInfo = await FlutterUpgrade.appInfo;
    // print(AppInfo);
    //   AppUpgrade.appUpgrade(
    //   context,
    //   _checkAppInfo(),
    //   iosAppId: 'id88888888',
    // );
    // }

    if (Platform.isIOS) return;

    // final packageInfo = await PackageInfo.fromPlatform();

    // VersionModel model = await versionViewModel.getData();

    // int currentVersion = int.parse(removeDot(packageInfo.version));

    // int netVersion = int.parse(removeDot(model.appVersion));

    // if (currentVersion >= netVersion) {
    //   debugPrint('当前版本是最新版本');
    //   return;
    // }

//    showDialog(
//      context: context,
//      builder: (ctx2) {
//        return UpdateDialog(
//          version: model.appVersion,
//          updateUrl: model.downloadUrl,
//          updateInfo: model.updateInfo,
//          isForce: model.force,
//        );
//      },
//    );
  }

// Future<AppUpgradeInfo> _checkAppInfo() async {
//   return Future.value(AppUpgradeInfo(
//     title: '新版本V1.1.1',
//     contents: [
//       '1、支持立体声蓝牙耳机，同时改善配对性能',
//       '2、提供屏幕虚拟键盘',
//       '3、更简洁更流畅，使用起来更快',
//       '4、修复一些软件在使用时自动退出bug',
//       '5、新增加了分类查看功能'
//     ],
//     apkDownloadUrl: '',
//     force: false,
//    ));
//   }

  @override
  Widget build(BuildContext context) {
    List<TabBarModel> pages = <TabBarModel>[
      new TabBarModel(
        title: DemoLocalizations.of(context).taskTitle,
        // icon: new LoadImage("assets/images/bottom_protect.png", false),
        // selectIcon: new LoadImage("assets/images/bottom_protect.png", true),
        icon: new LoadImage("assets/images/bottom_home.png", false),
        selectIcon: new LoadImage("assets/images/bottom_home.png", true),

        page: new HomePage(),
      ),
      new TabBarModel(
        title: DemoLocalizations.of(context).titleTowTitle,
        icon: new LoadImage("assets/images/bottom_home.png", false),
        selectIcon: new LoadImage("assets/images/bottom_home.png", true),
        page: new DrawerPage(),
      ),
    ];
    _login(context);
    return new RootTabBar(pages: pages, currentIndex: 0);
  }
}

class LoadImage extends StatelessWidget {
  final String img;
  final bool isSelect;

  LoadImage(this.img, [this.isSelect = false]);

  @override
  Widget build(BuildContext context) {
    return new Container(
      margin: EdgeInsets.only(bottom: 2.0),
      width: 30.0,
      height: 30.0,
      child: new Image.asset(
        img,
        fit: BoxFit.cover,
        gaplessPlayback: true,
        color: isSelect ? fixedColor : mainTextColor,
      ),
    );
  }
}
